//
//  StyleControl.m
//  whopaidforit
//
//  Created by Hoang Vu on 1/25/13.
//  Copyright (c) 2013 Hoang Vu. All rights reserved.
//

#import "StyleControl.h"

@implementation StyleControl

+(void)background:(UIView *)view
{
    view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg"]];
}

+(void )naviStyle :(UIView *)view andTitle: (NSString *)title andColor :(NSInteger)flagColor andleftButton :(UIButton *)left andRightButton :(UIButton *)right borderLeft:(int)bleft borderRight:(int)bright
{
    UINavigationBar *navi =[[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 40)];
    
    UILabel *label =[[UILabel alloc]initWithFrame:CGRectMake(63, 2, 200, 42)];
    label.text=title;
    label.font = [UIFont systemFontOfSize:20.0];
    [label setBackgroundColor:[UIColor clearColor ]];
    label.textAlignment=UITextAlignmentCenter ;
    
    //left.frame = CGRectMake(5, 8, 50, 33);
    //right.frame = CGRectMake(265, 8, 50, 33);

    
    if(flagColor)
    {
        [navi setBackgroundImage:[UIImage imageNamed:@"naviWhite"] forBarMetrics:UIBarMetricsDefault] ;
        label.textColor=[UIColor blackColor];
        if(bleft){
            [left setBackgroundImage:[UIImage imageNamed:@"bgbuttonwhite"] forState:UIControlStateNormal];
            [left.layer setMasksToBounds:YES];
            left.layer.borderColor = [UIColor blackColor].CGColor;
            left.layer.borderWidth = 1.3f;
            left.layer.cornerRadius = 8;
            [left setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            left.titleLabel.font = [UIFont systemFontOfSize:14];
        }
        if(bright){
            [right setBackgroundImage:[UIImage imageNamed:@"bgbuttonwhite"] forState:UIControlStateNormal];
            [right.layer setMasksToBounds:YES];
            right.layer.borderColor = [UIColor blackColor].CGColor;
            right.layer.borderWidth = 1.3f;
            right.layer.cornerRadius = 8;
            [right setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            right.titleLabel.font = [UIFont systemFontOfSize:14];
        }
        
    }
    else
    {
        [navi setBackgroundImage:[UIImage imageNamed:@"naviBlack"] forBarMetrics:UIBarMetricsDefault];
        label.textColor=[UIColor whiteColor];
        if(bleft){
            [left setBackgroundImage:[UIImage imageNamed:@"bgbuttonblack"] forState:UIControlStateNormal];
            [left.layer setMasksToBounds:YES];
            left.layer.borderColor = [UIColor blackColor].CGColor;
            left.layer.borderWidth = 1.3f;
            left.layer.cornerRadius = 8;
            [left setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            left.titleLabel.font = [UIFont systemFontOfSize:14];
        }
        if(bright){
            [right setBackgroundImage:[UIImage imageNamed:@"bgbuttonblack"] forState:UIControlStateNormal];
            [right.layer setMasksToBounds:YES];
            right.layer.borderColor = [UIColor blackColor].CGColor;
            right.layer.borderWidth = 1.3f;
            right.layer.cornerRadius = 8;
            [right setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            right.titleLabel.font = [UIFont systemFontOfSize:14];
        }
    }
    
    [navi addSubview:label];
    [label release];
    
    [navi addSubview:left];
    [navi addSubview:right];
    
    [view addSubview:navi ];
    [navi release];
}

+(void)setPersonalStyle:(id)sender BackgroundHeader:(int)bg{
    if ([sender isKindOfClass:[UITableViewController class]]) {
        UITableViewController *tableController = (UITableViewController*)sender;
        tableController.tableView.backgroundView = nil;
        tableController.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg"]];
    }
    if ([sender isKindOfClass:[UIViewController class]]) {
        UIViewController *viewController = (UIViewController*)sender;
        viewController.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg"]];
        if(bg == 1){
            [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"naviWhite"] forBarMetrics:UIBarMetricsDefault];
            //UIColor* navBarColor = [UIColor colorWithRed:1 green:0.996 blue:0.996 alpha:1.0];
            //[[UINavigationBar appearance] setTintColor:navBarColor];
            
            //[[UINavigationBar appearance] setBarStyle:UIBarStyleBlackTranslucent];
            //[[UINavigationBar appearance] setAlpha:0.7];
            [[UINavigationBar appearance] setTitleTextAttributes:
             [NSDictionary dictionaryWithObjectsAndKeys:
              [UIColor whiteColor],
              UITextAttributeTextColor,
              [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.8],
              UITextAttributeTextShadowColor,
              [NSValue valueWithUIOffset:UIOffsetMake(0, -1)],
              UITextAttributeTextShadowOffset,
              [UIFont fontWithName:@"STHeitiSC-Light" size:0.0], 
              UITextAttributeFont, nil]];
        }else{
            [[UINavigationBar appearance] setBackgroundImage:[UIImage imageNamed:@"naviBlack"] forBarMetrics:UIBarMetricsDefault];
            //[[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
        }
    }
}

+(UIButton*)makeButton:(NSString*)title andFont:(UIFont*) font andColor:(UIColor*)color andImage:(UIImage*)image andBackground:(UIImage*) backgroundImage andHightlightBackground:(UIImage*)hoverImage {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setBackgroundImage:backgroundImage forState:UIControlStateNormal];
    if(hoverImage) [button setBackgroundImage:hoverImage forState:UIControlStateHighlighted];
    if(image) [button setImage:image forState:UIControlStateNormal];
    [button setTitle:title forState:UIControlStateNormal];
    [button setTitleColor:color forState:UIControlStateNormal];
    [button.titleLabel setFont:font];
    [button sizeToFit];
    return button;
}

+(UIButton*)makePersonalBarButton{
    UIButton *button = [self makeButton:nil andFont:[UIFont boldSystemFontOfSize:12] andColor:[UIColor whiteColor] andImage:nil andBackground:[[UIImage imageNamed:@"btnnav"] stretchableImageWithLeftCapWidth:5 topCapHeight:0] andHightlightBackground:nil];
    return button;
}

+(void) drawLine:(UIView *)sender PositionX:(int)x PositionY:(int)y Width:(int)width
{
    
    UIImageView *line = [[UIImageView alloc] init];
    line.frame = CGRectMake(x, y, width, 1);
    UIImage *image = [UIImage imageNamed:@"line"];
    line.image = image;
    [sender addSubview:line];
    [line release];
}

+ (void) setLabelStyle:(UILabel *) label andText:(NSString *) text andTextAlignment:(NSString *) textalignment andFont:(UIFont *) font andTextColor:(UIColor *) textcolor andBackgroundColor:(UIColor *) backgroundcolor andShadowColor:(UIColor *) shadowcolor andShadowOffset:(CGSize) shadowoffset {
    if(text) label.text = text;
    if([textalignment isEqualToString:@"left"]){
        label.textAlignment = UITextAlignmentLeft;
    } else if([textalignment isEqualToString:@"right"]){
        label.textAlignment = UITextAlignmentRight;
    } else if([textalignment isEqualToString:@"center"]){
        label.textAlignment = UITextAlignmentCenter;
    }
    if(font) label.font = font;
    if(textcolor) label.textColor = textcolor;
    label.backgroundColor = [UIColor clearColor];
    if (backgroundcolor) label.backgroundColor = backgroundcolor;
    if(shadowcolor) label.shadowColor = shadowcolor;
    label.shadowOffset = shadowoffset;
}

+(NSString*) formatCurrency:(NSString*) string addCurency:(BOOL)currency{
    Event *event = [Event instance];
    Helper *helper = [Helper instance];
    NSString *symbol = [helper showSymbol:event.currency];
    NSNumberFormatter *formatter = [NSNumberFormatter new];
    [formatter setNumberStyle: NSNumberFormatterCurrencyStyle];
    NSLocale *locale_input = [[NSLocale alloc] initWithLocaleIdentifier:@"us_US"];
    NSLocale *locale_output = [[NSLocale alloc] initWithLocaleIdentifier:@"nl_NL"];
    [formatter setLocale:locale_input];
    currency == YES ? [formatter setCurrencySymbol:symbol] : [formatter setCurrencySymbol:@""];
    [formatter setLenient:YES];
    [formatter setGeneratesDecimalNumbers:YES];
    NSDecimalNumber *amount = (NSDecimalNumber*) [formatter numberFromString:[string stringByReplacingOccurrencesOfString:symbol withString:@""]];
    formatter.negativePrefix = [NSString stringWithFormat:@"%@-", formatter.currencySymbol];
    formatter.negativeSuffix = @"";
    [formatter setLocale:locale_output];
    [locale_output release];
    [locale_input release];
    string = [formatter stringFromNumber:amount];
    [formatter release];
    return string;
}

+(void) setViewStyle:(UIView *) view
{
    view.layer.borderColor = [UIColor colorWithRed:0.278 green:0.275 blue:0.275 alpha:1.0].CGColor;
    view.layer.borderWidth = 1.0f;
    view.layer.cornerRadius = 5;
    view.layer.shadowOpacity = 1;
    [view.layer setShadowOffset:CGSizeMake(-0.2, -0.2)];
    [view.layer setShadowRadius:3];
}
+(void) setBorderStyle:(UIImageView *) view
{
    
    [view.layer setMasksToBounds:YES];
    view.layer.borderColor = [UIColor colorWithRed:0.278 green:0.275 blue:0.275 alpha:1.0].CGColor;
    view.layer.borderWidth = 1.0f;
    view.layer.cornerRadius = 5;
}

+(void) setPriceStyle:(UILabel *) view Price:(double)price
{
    if(price < 0)
        view.textColor = [UIColor colorWithRed:225/255.0f green:44/255.0f blue:0/255.0f alpha:1];
    else
        view.textColor = [UIColor colorWithRed:63/255.0f green:134/255.0f blue:196/255.0f alpha:1];
    view.text = [self formatCurrency:[NSString stringWithFormat:@"%.2f",price] addCurency:YES];
    UIFont *font = [UIFont fontWithName:@"Georgia" size:15.0f];
    [view setFont:font];
}
+(void) setPriceStyle:(UILabel *) view
{
    UIFont *font = [UIFont fontWithName:@"Georgia" size:15.0f];
    view.textColor = [UIColor colorWithRed:63/255.0f green:134/255.0f blue:196/255.0f alpha:1];
    [view setFont:font];
}
+(void) setPriceStyleTextField:(UITextField *) view
{
    UIFont *font = [UIFont fontWithName:@"Georgia" size:15.0f];
    view.textColor = [UIColor colorWithRed:63/255.0f green:134/255.0f blue:196/255.0f alpha:1];
    [view setFont:font];
}
+(void) setDateStyle:(UILabel *) view
{
    UIFont *font = [UIFont fontWithName:@"Courier-Oblique" size:12.0f];
    [view setFont:font];
}
+(void) setDateStyleTextField:(UITextField *) view
{
    UIFont *font = [UIFont fontWithName:@"Courier-Oblique" size:12.0f];
    [view setFont:font];
}
@end
